package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R save(@RequestBody Category category){
        log.info("进行分类添加操作, category = {}", category.toString());
        categoryService.save(category);
        return R.success();
    }

    @GetMapping("/page")
    public R<Page<Category>> page(@RequestParam(required = false, defaultValue = "1") Integer page,
                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize){
        log.info("执行分页查询 , page={}, pageSize = {}",  page, pageSize);

        //构建分页参数Page
        Page<Category> pageInfo = new Page<>(page,pageSize);

        //查询及排序条件
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(Category::getSort);

        //执行分页查询
        categoryService.page(pageInfo, lambdaQueryWrapper);

        //返回结果
        return R.success(pageInfo);
    }



    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id){
        log.info("删除分类数据 , id = {}", id);

        //categoryService.removeById(id);
        categoryService.remove(id);

        return R.success();
    }

    /**
     * 根据ID查询分类
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Category> getById(@PathVariable Long id){
        log.info("根据ID查询分类数据, id = {}",id);
        Category category = categoryService.getById(id);
        if(category != null){
            return R.success(category);
        }
        return R.error("该分类数据不存在!");
    }

    /**
     * 根据ID修改分类数据
     * @param category
     * @return
     */
    @PutMapping
    public R update(@RequestBody Category category){
        log.info("根据ID修改分类数据 , category= {}", category.toString());

        categoryService.updateById(category);

        return R.success();
    }


    /**
     * 查询分类列表
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        //组装查询条件
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(category.getType() != null, Category::getType, category.getType());
        lambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime); // order by sort asc , update_time desc ;

        //执行查询
        List<Category> categoryList = categoryService.list(lambdaQueryWrapper);

        //返回结果
        return R.success(categoryList);
    }

}
